/********************************************************************************/
/*A*/
/*4. CLASS EXERCISES */

/*4.1*/
/* Write queries to answer the following questions */


/* In which year was the movie American Beauty released? */
movie(american_beauty,Y).

/*Find a movie that was released before 2000? */
moviebef2000(M,Y):- movie(M,Y),Y<2000.

/* Find the name and year of a movie? */
movie(M,Y).

/* Find an actor who has appeared in more than one movie? */
actorMoreOne(A):- actor(M1,A,_),actor(M2,A,_),M1 \== M2.

/* Find a director who has directed a movie in which the actress Scarlett Johansson appeared? */
findDirector(M,D):-director(M,D),actress(M,scarlett_johansson,_).

/*Note: show the same object in the multible line*/
/*!Find an actor who has also directed a movie? */
findActor(A):-actor(_,A,_),director(_,A).

/*!Find an actor or actress who has also directed a movie? */
findAD(A):-(actor(_,A,_),director(_,A));(actress(_,A,_),director(_,A)).


/*4.2*/
/*Write a definition in Prolog for each of the following predicates*/

/*released_since(M,Y) <- movie M was released after year Y*/
released_since(M,Y):- movie(M,Y), Y > 2000.

/*released_between(M,Y1,Y2) <- movie M was released between year Y1 and year Y2 inclusive*/
released_between(M,Y1,Y2):-movie(M,Y),Y=<Y2, Y>= Y1.

/*same_year_as(M1,M2) <- movie M1 was released in the same year as movie M2 */
same_year_as(M1,M2):-movie(M1,Y),movie(M2,Y),M1 \== M2.

/*newer(M1,M2) <- movie M1 was released after movie M2 */
newer(M1,M2):-movie(M1,Y1),movie(M2,Y2),Y1>Y2.


/*cast_member(A,M) <- person A was an actor or actress in movie M */
cast_member(A,M) :- actor(M,A,_);actress(M,A,_).

/*directed_by(X,Y) <- person X has been in a movie directed by Y */




/********************************************************************************/
/*B*/
/*4. CLASS EXERCISES */
/*4.1*/
sum([],0).
sum([H|T],X):- sum(T,Y),X is Y + H.

/*4.2*/
applast([],Y,[Y]):-!.
applast([H|T],Y,[H|X]):- applast(T,Y,X).

/*Fail??????????????????????????*/
applast1([],Y,[Y]):-!.
applast1([H|T],Y,X):- applast1(T,Y,X1), X is [H|X1].

/*???? warning assgin varible...toan tu in prolog*/
/*is ??== @==*/
a(N,X):- X is [5,6].
/*the result only show 5. why being [5,6].*/

/*4.4*/
%insertFirst(N,L,[N|L]):- !.
%insertLast(N,[],[N]):-!.
%insertLast(N,[H|T],L):-insertLast(N,T,L1), L @= [H|L1].



makelist(1,[1]):- !.
makelist(N,X):- M is N - 1,  makelist(M,X1), insertLast(N,X1,X).


/*4.5*/

div2(N,M):- mod(N,M) =\= 0.
divN(N,M):- N =:= M+1, 1.
divN(N,M):- K is M + 1, K < N, div2(N,K),divN(N,K).





divisible(X, Y):-
	N is Y*Y, N =< X, X mod Y =:= 0.

divisible(X, Y):-
	 Y < X, Y1 is Y + 1, divisible(X, Y1).

checkPrime(X):-
	Y is 2, X > 1 , \+divisible(X, Y).


/*Extra*/
/*1. insertFirstList(N,L)-insert a interger into first element of list.*/
/*2. insertLastList(N,L)-insert a interger into last element of list.*/
/*3. sortList(L)-sort a list by decrease*/

%sortList([H|T]):- 
getFirst([],null).
getFirst([H|T],H).

getSubList([],[]):- !.
getSubList([H|T],T).

insertFirst(N,L,[N|L]):- !.
insertLast(N,[],[N]):-!.
insertLast(N,[H|T],L):-insertLast(N,T,L1), L @= [H|L1].

/**/
insertSort(N,[],[N]):-!.
insertSort(N,[H],L):- N >= H, L@= [H,N];L @= [N,H],!.
insertSort(N,[X1,X2],L):- N =< X1,X1 =< X2,L@=[N,X1,X2],!.
insertSort(N,[X1,X2],L):- N =< X2,X2 =< X1,L@=[N,X2,X1],!.
insertSort(N,[X1,X2],L):- N >= X2,X1 =< X2,L@=[X1,X2,N],!.
insertSort(N,[X1,X2],L):- N >= X1,N =< X2,L@=[X1,N,X2],!.
insertSort(N,[X1,X2],L):- N >= X1,X2 =< X1,L@=[X2,X1,N],!.
insertSort(N,[X1,X2],L):- N >= X2,N =< X1,L@=[X2,N,X1],!.
%insertSort(5,[4,6,3,7],L).
%getFirst([4,6,3,7],X).

insertSort(N,[H|T],L):- 
					insertSort(N,T,L1),
					getFirst(L1,X0),
					X0 < H, insertSort(H,L1,L2),						
					L@= L2.
insertSort(N,[H|T],L):- 
					insertSort(N,T,L1),
					getFirst(L1,X0),
					X0 >= H, insertFirst(H,L1,L2),						
					L@= L2.
										
					
					
									



				

/**/

sortList([],[]):- !.
sortList([H],[H]):- !.
sortList([X1,X2],L):- X1 >= X2, L @= [X2,X1],!.
sortList([X1,X2],L):- X1 < X2, L @= [X2,X1],!.

sortList([H|T],L):- sortList(T,L1), insertSort(H,L1,L2), L @= L2.






/*4. sortList(L)-sort a list by andecrease*/
/*5. findMaxList(L,M)-find a element in list that is max.*/

/*6. findMinList(L,M)-find a element in list that is min.*/
/*7. */

